import matplotlib.pyplot as plt
import numpy as np
import os

'''
    第i张图画不同方法的i阶基（i为1,2,3,4,5,6）
'''


def plotAirfoilGeom(x, y_list, labels, save_path):
    '''
        在同一张图上展示一系列翼型
    :param coordinates:
    :param file_path:
    :return:
    '''

    # plt.rc('font', family='Latin Modern Roman')
    plt.rc('font', family='Times New Roman')

    linestyle_list = ['solid',
                      ':', '--', '-.',
                      ':', '--', '-.']
    color_list = ["black", "red", "blue", "red", "blue", "red", "blue"]
    long = 7
    height = 6.6

    fig = plt.figure(figsize=(long, height), dpi=80)
    fig.canvas.draw()

    for i in range(y_list.shape[1]):
        plt.plot(x, y_list[:, i],
                 linewidth=1,
                 linestyle=linestyle_list[i],
                 label=labels[i],
                 c=color_list[i])

    font = {'weight': 'normal',
            'size': 16}

    # 横纵坐标标题
    plt.xlabel("X/C", font)
    plt.ylabel("Y/C", font)

    font1 = 13
    # 横纵坐标刻度
    plt.xticks(size=font1)
    plt.yticks(size=font1)

    # plt.xlim((-0.01, 1.01))
    # plt.ylim((-0.07, 0.07))
    plt.legend(fontsize=font1)  # , bbox_to_anchor=(0.63, 0.43))
    plt.grid()

    plt.savefig(save_path, dpi=1000, bbox_inches='tight')
    plt.close()


if __name__ == "__main__":

    x = np.loadtxt("basis_data/x.dat", delimiter=' ')[:, 0]

    y1 = np.loadtxt("basis_data/POD_basis_NOML.csv", delimiter=',')[:, 0:6]
    y2 = np.loadtxt("basis_data/POD_basis_LGB.csv", delimiter=',')[:, 0:6]
    y3 = np.loadtxt("basis_data/POD_basis_XGB.csv", delimiter=',')[:, 0:6]
    y4 = np.loadtxt("basis_data/POD_basis_GBDT.csv", delimiter=',')[:, 0:6]

    y_list = [y1, y2, y3, y4]
    save_names = ["1st", "2nd", "3rd", "4th", "5th", "6th"]

    for i in range(6):  # 1-6阶基
        y_i = np.empty(shape=(301, len(y_list)))
        for j, y in enumerate(y_list):
            y_i[:, j] = y[:, i]
        save_path = r"basis_data/{}_Basis.png".format(save_names[i])
        labels = ["Origin", "LGB", "XGB", "GBDT"]
        plotAirfoilGeom(x, y_i, labels, save_path)
        print("二维翼型可视化图已保存在{}".format(save_path))
